package com.hotchpotch.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hotchpotch.R;
import com.hotchpotch.adapter.EncryptAdapter;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.inter.OnRecyclerViewItemClickListener;
import com.hotchpotch.util.EncryptUtils;

/**
 * Created by Administrator on 2017/7/12 0012.
 */

public class EncryptAndDecryptActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private EncryptAdapter adapter;
    private EditText editText;
    private TextView textView;
    private Button btnEncrypt, btnDecrypt, btnCopy;
    private int position = 0;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    doEncrypt(position);
                    break;
                case 1:
                    doDecrypt(position);
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_encrypt_decrypt;
    }

    @Override
    protected void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        adapter = new EncryptAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        editText = (EditText) findViewById(R.id.edit);
        textView = (TextView) findViewById(R.id.text);
        btnEncrypt = (Button) findViewById(R.id.btn_encrypt);
        btnDecrypt = (Button) findViewById(R.id.btn_decrypt);
        btnCopy = (Button) findViewById(R.id.copy);
    }

    @Override
    protected void initEvents() {
        btnEncrypt.setOnClickListener(clickListener);
        btnDecrypt.setOnClickListener(clickListener);
        btnCopy.setOnClickListener(clickListener);
    }

    @Override
    protected void initData() {
        adapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) {
                position = recyclerView.getChildAdapterPosition(view);
                adapter.setPosition(position);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onItemLongClick(View view) {

            }
        });
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            closeKeyboard();
            switch (view.getId()) {
                case R.id.btn_encrypt://加密
                    handler.sendEmptyMessage(0);
                    break;
                case R.id.btn_decrypt://解密
                    handler.sendEmptyMessage(1);
                    break;
                case R.id.copy://复制结果
                    copyToClipboard(EncryptAndDecryptActivity.this, textView.getText().toString().trim());
                    break;
            }
        }
    };

    /**
     * 加密
     *
     * @param pos
     */
    private void doEncrypt(int pos) {
        if (TextUtils.isEmpty(editText.getText().toString().trim())) {
            showToast("输入的内容不能为空");
            return;
        }
        switch (pos) {
            case 0://md5
                textView.setText(EncryptUtils.getMD5(editText.getText().toString().trim()));
                break;
            case 1://des
                break;
            case 2://base64
                textView.setText(EncryptUtils.encryptBASE64(editText.getText().toString().trim().getBytes()));
                break;
        }
    }

    /**
     * 解密
     *
     * @param pos
     */
    private void doDecrypt(int pos) {
        if (TextUtils.isEmpty(editText.getText().toString().trim())) {
            showToast("输入的内容不能为空");
            return;
        }
        switch (pos) {
            case 0://md5
                showToast("MD5是不可逆的！");
                break;
            case 1://des
                break;
            case 2://base64
                textView.setText(EncryptUtils.decryptBASE64(editText.getText().toString().trim()).toString());
                break;
        }
    }
}
